var parentMenuTree;
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    initAddWindow();
    //去除所有控件的虚线框
	removeBlur();
});

/**
 * 初始化添加窗口
 */
function initAddWindow(){
	//初始化菜单的所属父类（先点击菜单树，后点击添加菜单的情况）
	if(menuName){
		$("#menuParentName").val(menuName);
		$("#parentId").val(menuId);
	}
	//获取父菜单树
	parentMenuTree = $.fn.zTree.getZTreeObj('parentMenuTree');
	//添加父菜单树的回调事件
	parentMenuTree.setting.callback.onClick = onDialogTreeOnClick("menuParentName","parentId");
	//获取查看权限树
	var lookAuthorityMenuTree = $.fn.zTree.getZTreeObj('lookAuthorityMenuTree');
	//添加查看权限树的回调事件
	lookAuthorityMenuTree.setting.callback.onClick = onDialogTreeOnClick("menuLookAuthority","authorityId");
	//获取操作权限树
	post(getPath() + "base/authority/tree",{pid:0},function(treeNodes){
		treeNodes.splice(0,1);//去掉第一个节点
		$.fn.zTree.init($("#operateAuthorityTree"),operateAuthority_setting,treeNodes); 
		var operateAuthorityMenuTree = $.fn.zTree.getZTreeObj('operateAuthorityTree');
		//取消所有已勾选的节点
		operateAuthorityMenuTree.checkAllNodes(false);
		//添加操作权限树的回调事件
		operateAuthorityMenuTree.setting.callback.onCheck = onZTreeCheck("authority","operateAuthorityIds");
	});
	//初始化点击事件
	initClick();
	//保存菜单
	saveMenu();
}

//初始化点击事件
function initClick(){
	//点击菜单所属父类
	$("#menuParentName").bind("click", {menuContentId:"parentMenu"}, showMenu).bind("focus", function(){
		parentMenuTree.expandNode(parentMenuTree.getNodeByTId("1"), true);
		//取消所有已勾选的节点
		parentMenuTree.checkAllNodes(false);
		var parentMenuNode = parentMenuTree.getNodeByParam("id", menuId);
		parentMenuTree.selectNode(parentMenuNode, false);
	});
	//点击查看权限
	$("#menuLookAuthority").click({menuContentId:"lookAuthorityMenu"}, showMenu);
	//点击操作权限
	//$("#authority").click({menuContentId:"operateAuthorityMenu"}, showMenu);
}

//保存菜单
function saveMenu(){
	 //声明消息提示
    var toolTips = new Object();
    //设置表单验证
    var validator = $("#addForm").validate({
        rules:{
        	label:"required",
        	menuParentName:"required",
        	//className:"required",
        	controlHeight:"required",
        	sortFlag:"required",
        	menuLookAuthority:"required"
        },
        messages:{
        	label:"请输入菜单名称",
        	menuParentName:"请选择所属父类",
        	//className:"请输入菜单样式",
        	controlHeight:"请输入菜单高度",
        	sortFlag:"请输入菜单序号",
        	menuLookAuthority:"请选择菜单查看权限"
        },
        errorPlacement: function(eror,element){
            var toolTip = createTooltip(toolTips,element.attr("id"));
            toolTip.setContent(eror.text());
            element.addClass("error-border");

        },
        success:function(label){
            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
            var toolTip = toolTips[elementId];
            $("#" + elementId).removeClass("error-border");
            toolTip.close();
            toolTip.detach();
        },
        submitHandler:function(form){
        	loadDiv.showLoading();
            post(getPath() + "base/menu/add",$("#addForm").serialize(),function(resultObject){
            	loadDiv.hideLoading();
            	var errors = resultObject.errors;
                //判断后台是否有返回错误消息
                if(errors && errors.length > 0){
                    for(var i=0; i < errors.length; i++){
                        var error = errors[i];
                        var toolTip = createTooltip(toolTips,error.field);
                        toolTip.setContent(error.defaultMessage);
                        $("#" + error.field).addClass("error-border");
                    }
                }else{
                    var item = resultObject.item;
                    var parentNode = getNodeById('parentMenuTree', item.parentId);
                    var menuparentNode = getNodeById('menuTree', item.parentId);
                    if(parentNode){
                        var newNode = {};
                        newNode.id = item.id;
                        newNode.pId = item.parentId;
                        newNode.name = item.label;
                        newNode.title = item.label;
                        var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');parentMenuTree
                        zTree.addNodes(parentNode, newNode);
                        var zTree = $.fn.zTree.getZTreeObj('menuTree');
                        zTree.addNodes(menuparentNode, newNode);
                        zTree.expandNode(menuparentNode,true);
                    }
                    $(".grid-list").html("");
                    $("#title").text("菜单管理");
                    //添加成功，弹出添加成功提示框，当用户点击确定后刷新页面
                    createWindow("添加成功");
                    $(".grid-list").load(getPath() + "base/menu/add");
                }
            },function(resultObject){
            	loadDiv.hideLoading();
        		createWindow(resultObject).dialog('open');
        	});
        }
    })
}

function heavyLoad(){
	$(".grid-list").load(getPath() + "base/menu/add");
}